package com.huatai.common.util;

/**
 * @program: ht-system
 * @description: 文件工具类
 * @author: songxiude
 * @create: 2023-01-31 15:26
 **/

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.PatternMatchUtils;
import org.springframework.web.multipart.MultipartFile;

public class FileUtil extends FileCopyUtils {
    public FileUtil() {
    }

    public static List<File> list(String path) {
        File file = new File(path);
        return list((File)file, (FileFilter)FileUtil.TrueFilter.TRUE);
    }

    public static List<File> list(String path, final String fileNamePattern) {
        File file = new File(path);
        return list(file, (pathname) -> {
            String fileName = pathname.getName();
            return PatternMatchUtils.simpleMatch(fileNamePattern, fileName);
        });
    }

    public static List<File> list(String path, FileFilter filter) {
        File file = new File(path);
        return list(file, filter);
    }

    public static List<File> list(File file) {
        List<File> fileList = new ArrayList();
        return list(file, fileList, FileUtil.TrueFilter.TRUE);
    }

    public static List<File> list(File file, final String fileNamePattern) {
        List<File> fileList = new ArrayList();
        return list(file, fileList, (pathname) -> {
            String fileName = pathname.getName();
            return PatternMatchUtils.simpleMatch(fileNamePattern, fileName);
        });
    }

    public static List<File> list(File file, FileFilter filter) {
        List<File> fileList = new ArrayList();
        return list(file, fileList, filter);
    }

    private static List<File> list(File file, List<File> fileList, FileFilter filter) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                File[] var4 = files;
                int var5 = files.length;

                for(int var6 = 0; var6 < var5; ++var6) {
                    File f = var4[var6];
                    list(f, fileList, filter);
                }
            }
        } else {
            boolean accept = filter.accept(file);
            if (file.exists() && accept) {
                fileList.add(file);
            }
        }

        return fileList;
    }

    public static String getFileExtension(String fullName) {
        Assert.notNull(fullName, "file fullName is null.");
        String fileName = (new File(fullName)).getName();
        int dotIndex = fileName.lastIndexOf(46);
        return dotIndex == -1 ? "" : fileName.substring(dotIndex + 1);
    }

    public static String getNameWithoutExtension(String file) {
        Assert.notNull(file, "file is null.");
        String fileName = (new File(file)).getName();
        int dotIndex = fileName.lastIndexOf(46);
        return dotIndex == -1 ? fileName : fileName.substring(0, dotIndex);
    }

    public static String getTempDirPath() {
        return System.getProperty("java.io.tmpdir");
    }

    public static File getTempDir() {
        return new File(getTempDirPath());
    }

    public static String readToString(final File file) {
        return readToString(file, Charsets.UTF_8);
    }

    public static String readToString(final File file, final Charset encoding) {
        try {
            InputStream in = Files.newInputStream(file.toPath());
            Throwable var3 = null;

            String var4;
            try {
                var4 = IoUtil.readToString(in, encoding);
            } catch (Throwable var14) {
                var3 = var14;
                throw var14;
            } finally {
                if (in != null) {
                    if (var3 != null) {
                        try {
                            in.close();
                        } catch (Throwable var13) {
                            var3.addSuppressed(var13);
                        }
                    } else {
                        in.close();
                    }
                }

            }

            return var4;
        } catch (IOException var16) {
            throw Exceptions.unchecked(var16);
        }
    }

    public static byte[] readToByteArray(final File file) {
        try {
            InputStream in = Files.newInputStream(file.toPath());
            Throwable var2 = null;

            byte[] var3;
            try {
                var3 = IoUtil.readToByteArray(in);
            } catch (Throwable var13) {
                var2 = var13;
                throw var13;
            } finally {
                if (in != null) {
                    if (var2 != null) {
                        try {
                            in.close();
                        } catch (Throwable var12) {
                            var2.addSuppressed(var12);
                        }
                    } else {
                        in.close();
                    }
                }

            }

            return var3;
        } catch (IOException var15) {
            throw Exceptions.unchecked(var15);
        }
    }

    public static void writeToFile(final File file, final String data) {
        writeToFile(file, data, Charsets.UTF_8, false);
    }

    public static void writeToFile(final File file, final String data, final boolean append) {
        writeToFile(file, data, Charsets.UTF_8, append);
    }

    public static void writeToFile(final File file, final String data, final Charset encoding) {
        writeToFile(file, data, encoding, false);
    }

    public static void writeToFile(final File file, final String data, final Charset encoding, final boolean append) {
        try {
            OutputStream out = new FileOutputStream(file, append);
            Throwable var5 = null;

            try {
                IoUtil.write(data, out, encoding);
            } catch (Throwable var15) {
                var5 = var15;
                throw var15;
            } finally {
                if (out != null) {
                    if (var5 != null) {
                        try {
                            out.close();
                        } catch (Throwable var14) {
                            var5.addSuppressed(var14);
                        }
                    } else {
                        out.close();
                    }
                }

            }

        } catch (IOException var17) {
            throw Exceptions.unchecked(var17);
        }
    }

    public static void toFile(MultipartFile multipartFile, final File file) {
        try {
            toFile(multipartFile.getInputStream(), file);
        } catch (IOException var3) {
            throw Exceptions.unchecked(var3);
        }
    }

    public static void toFile(InputStream in, final File file) {
        try {
            OutputStream out = new FileOutputStream(file);
            Throwable var3 = null;

            try {
                copy(in, out);
            } catch (Throwable var13) {
                var3 = var13;
                throw var13;
            } finally {
                if (out != null) {
                    if (var3 != null) {
                        try {
                            out.close();
                        } catch (Throwable var12) {
                            var3.addSuppressed(var12);
                        }
                    } else {
                        out.close();
                    }
                }

            }

        } catch (IOException var15) {
            throw Exceptions.unchecked(var15);
        }
    }

    public static void moveFile(final File srcFile, final File destFile) throws IOException {
        Assert.notNull(srcFile, "Source must not be null");
        Assert.notNull(destFile, "Destination must not be null");
        if (!srcFile.exists()) {
            throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
        } else if (srcFile.isDirectory()) {
            throw new IOException("Source '" + srcFile + "' is a directory");
        } else if (destFile.exists()) {
            throw new IOException("Destination '" + destFile + "' already exists");
        } else if (destFile.isDirectory()) {
            throw new IOException("Destination '" + destFile + "' is a directory");
        } else {
            boolean rename = srcFile.renameTo(destFile);
            if (!rename) {
                copy(srcFile, destFile);
                if (!srcFile.delete()) {
                    deleteQuietly(destFile);
                    throw new IOException("Failed to delete original file '" + srcFile + "' after copy to '" + destFile + "'");
                }
            }

        }
    }

    public static boolean deleteQuietly(@Nullable final File file) {
        if (file == null) {
            return false;
        } else {
            try {
                if (file.isDirectory()) {
                    FileSystemUtils.deleteRecursively(file);
                }
            } catch (Exception var3) {
            }

            try {
                return file.delete();
            } catch (Exception var2) {
                return false;
            }
        }
    }

    public static class TrueFilter implements FileFilter, Serializable {
        private static final long serialVersionUID = -6420452043795072619L;
        public static final TrueFilter TRUE = new TrueFilter();

        public TrueFilter() {
        }

        public boolean accept(File pathname) {
            return true;
        }
    }
}

